package com.yibo.feign.message;

import com.yibo.feign.FailHandler;
import com.yibo.feign.RestHelper;
import com.yibo.feign.RestModel;
import com.yibo.support.helper.JsonHelper;
import com.yibo.support.pojo.IResult;
import com.yibo.support.pojo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

import static com.yibo.feign.ServerContext.MESSAGE;
import static com.yibo.feign.constant.Constant.REQUEST_PARM;
import static com.yibo.support.constant.ApiUrlConstant.PUBLIC_URL;

@Component
public class CheckWordServer {

    private static final Logger LOGGER = LoggerFactory.getLogger(CheckWordServer.class);

    @Autowired
    private RestTemplate  restTemplate;

    @SuppressWarnings("unchecked")
    public IResult<String>  emailCheckWord(String email, String type, FailHandler handler){
        //url
        String url = MESSAGE+PUBLIC_URL+"/checkWord/email/checkWord";
        //参数
        Map<String,Object>  data = new HashMap<>();
        data.put("email",email);
        data.put("type",type);
        //请求体
        RestModel<Result> restModel = RestHelper.restModel(url,HttpMethod.POST,data);
        //请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(REQUEST_PARM));
        //请求
        try {
            Object ret =  RestHelper.rest(restModel, headers);
            return result((ret));
        }catch (Exception e){
            LOGGER.info(e.getMessage(),e);
            handler.error(restModel);
        }
        return null;
    }


    @SuppressWarnings("unchecked")
    public IResult<String>  emailCheckWord(String email, String type){
        //url
        String url = MESSAGE+PUBLIC_URL+"/checkWord/email/checkWord";
        //参数
        Map<String,Object>  data = new HashMap<>();
        data.put("email",email);
        data.put("type",type);
        //请求体
        RestModel<Result> restModel =RestHelper.restModel(url,HttpMethod.POST,data);
        //请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(REQUEST_PARM));
        //请求
        try {
            Object ret =  RestHelper.rest(restModel, headers);
            return result((ret));
        }catch (Exception e){
            //无异常
            LOGGER.info(e.getMessage(),e);
        }
        return null;
    }


    @SuppressWarnings("unchecked")
    public IResult<String>  deleteCheckWord(String email, String type){
        //url
        String url = MESSAGE+PUBLIC_URL+"/checkWord/email/delete";
        //参数
        Map<String,Object>  data = new HashMap<>();
        data.put("email",email);
        data.put("type",type);
        //请求体
        RestModel<Result> restModel =RestHelper.restModel(url,HttpMethod.POST,data);
        //请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(REQUEST_PARM));
        //请求
        try {
            Object ret =  RestHelper.rest(restModel, headers);
            return result((ret));
        }catch (Exception e){
            //无异常
            LOGGER.info(e.getMessage(),e);
        }
        return null;
    }


    @SuppressWarnings("unchecked")
    private <T>Result<T>  result(Object ret){
        if(ret instanceof Map){
            return JsonHelper.coverObject(ret,Result.class,String.class);
        }
        return null;
    }



}
